package com.qfedu.Action;

import com.qfedu.biz.GoodsBiz;
import com.qfedu.biz.ShopCarBiz;
import com.qfedu.entity.CustomerEntity;
import com.qfedu.entity.GoodsEntity;
import com.qfedu.entity.ShopCarEntity;
import com.qfedu.util.Ajaxd;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

//表示都是ajax请求
@RestController
public class ShopCatAction {
    @Autowired
    private GoodsBiz goodsBiz;
    @Autowired
    private ShopCarBiz shopCarBiz;

    @RequestMapping("/addShopCar.do")
    public Ajaxd doAddShopCar(GoodsEntity goodsEntity, HttpServletRequest request){
        Ajaxd ajaxd = new Ajaxd();
        if (goodsEntity.getGid()==null ||goodsEntity.getGid()==""){
            ajaxd.setFlag(false);
            ajaxd.setMessage("添加失败");
            return ajaxd;
        }
        //查询商品
        List<GoodsEntity> goodsEntities = goodsBiz.queryAllGoods(goodsEntity);
        GoodsEntity goodsEntity1 = goodsEntities.get(0);
        //获取session
        HttpSession session = request.getSession();
        Map<String,GoodsEntity> shopCar =(Map<String, GoodsEntity>) session.getAttribute("shopCar");
        int thecount=0;
        int count=0;
        float allprice=0;
        float theprice=0;
        Boolean flag=true;

        CustomerEntity customerinfo = (CustomerEntity)session.getAttribute("customerinfo");
        Map<String,Object> map=new HashMap<String,Object>();

        //判断是否是第一次添加到购物车
        if (shopCar.size()==0){
            shopCar=new HashMap<String,GoodsEntity>();
            shopCar.put(goodsEntity1.getGid(),goodsEntity1);
            allprice=goodsEntity1.getGdiscount() * goodsEntity1.getGprice() * goodsEntity1.getGoodscount();
            theprice=allprice;
            String str=1+","+allprice+","+1+","+theprice;
            ajaxd.setFlag(true);
            ajaxd.setMessage(str);
            session.setAttribute("shopCar",shopCar);


            //数据库同步

            if (customerinfo!=null){
                //获取用户购物车
                ShopCarEntity shopCarEntity = shopCarBiz.queryShopCar(customerinfo.getCid());
                //先删除购物车商品
//                Boolean aBoolean = shopCarBiz.deleteByShopid(shopCarEntity.getScid());

                map.put("scid",shopCarEntity.getScid());
                map.put("gid",goodsEntity1.getGid());
                map.put("gcount",goodsEntity1.getGoodscount());
                Boolean aBoolean = shopCarBiz.insertShopcarAndGoods(map);

            }




        }else{
            Collection<GoodsEntity> values = shopCar.values();
            Iterator<GoodsEntity> iterator = values.iterator();
            while(iterator.hasNext()){
                GoodsEntity ge=iterator.next();
                //如果所添加的商品已存在则数目加一
                if (ge.getGid().equals(goodsEntity1.getGid())){
                    ge.setGoodscount(ge.getGoodscount()+1);
                    //要加入商品的总数目，总价格
                    theprice=ge.getGdiscount() * ge.getGprice() * ge.getGoodscount();
                    thecount=ge.getGoodscount();

                    //同步数据库
                    if (customerinfo!=null) {
                        ShopCarEntity shopCarEntity = shopCarBiz.queryShopCar(customerinfo.getCid());
                        map.put("scid",shopCarEntity.getScid());
                        map.put("gid",ge.getGid());
                        map.put("gcount",ge.getGoodscount());
                        Boolean aBoolean = shopCarBiz.updateShopcar(map);
                    }


                    flag=false;
                }
                //计算未加入新商品之前的总数目和总价格
                allprice+=ge.getGdiscount() * ge.getGprice() * ge.getGoodscount();
                count++;
            }
            //加入新商品之后的总数目和总价格
            if (flag){
                shopCar.put(goodsEntity1.getGid(),goodsEntity1);
                allprice+=goodsEntity1.getGdiscount() * goodsEntity1.getGprice() * goodsEntity1.getGoodscount();
                count++;
                thecount=1;
                theprice=goodsEntity1.getGprice();

                //同步数据库
                if (customerinfo!=null){
                    //获取用户购物车
                    ShopCarEntity shopCarEntity = shopCarBiz.queryShopCar(customerinfo.getCid());
                    //先删除购物车商品
//                Boolean aBoolean = shopCarBiz.deleteByShopid(shopCarEntity.getScid());

                    map.put("scid",shopCarEntity.getCid());
                    map.put("gid",goodsEntity1.getGid());
                    map.put("gcount",goodsEntity1.getGoodscount());
                    Boolean aBoolean = shopCarBiz.insertShopcarAndGoods(map);

                }




            }
            String str=count+","+allprice+","+thecount+","+theprice;
            ajaxd.setFlag(true);
            ajaxd.setMessage(str);
            session.setAttribute("shopCar",shopCar);
        }
        session.setAttribute("con01",count);
        session.setAttribute("con02",allprice);



        return ajaxd;

    }





    //减少商品数量请求
    @RequestMapping("/deleteGoods.do")
    public Ajaxd doDeleteGoods(GoodsEntity goodsEntity,HttpServletRequest request){
        Ajaxd ajaxd = new Ajaxd();
        //分为两种情况
        //1、当商品数量大于1时
        int count=0;
        float price=0;

        HttpSession session = request.getSession();

        CustomerEntity customerinfo = (CustomerEntity)session.getAttribute("customerinfo");
        Map<String,Object> map=new HashMap<String,Object>();


        Map<String,GoodsEntity> shopCar =(Map<String,GoodsEntity>) session.getAttribute("shopCar");
        GoodsEntity goodsEntity1 = shopCar.get(goodsEntity.getGid());
        //判断商品数量是否大于一
        if (goodsEntity1.getGoodscount()>1){
            goodsEntity1.setGoodscount(goodsEntity1.getGoodscount()-1);
            shopCar.put(goodsEntity1.getGid(),goodsEntity1);
            price=goodsEntity1.getGoodscount()*goodsEntity1.getGprice()*goodsEntity1.getGdiscount();
             count=goodsEntity1.getGoodscount();
            String str = jiesuan(shopCar);
            String[] split = str.split(",");
            session.setAttribute("con01",split[0]);
            session.setAttribute("con02",split[1]);
            str=str+","+count+","+price;
            ajaxd.setFlag(true);
            ajaxd.setMessage(str);
            session.setAttribute("shopCar",shopCar);

            //同步数据库
            if (customerinfo!=null) {
                ShopCarEntity shopCarEntity = shopCarBiz.queryShopCar(customerinfo.getCid());
                map.put("scid",shopCarEntity.getScid());
                map.put("gid",goodsEntity1.getGid());
                map.put("gcount",goodsEntity1.getGoodscount());
                Boolean aBoolean = shopCarBiz.updateShopcar(map);
            }


        }else{
            //若为最后一件商品则移除
            shopCar.remove(goodsEntity1.getGid());
            String str = jiesuan(shopCar);
            String[] split = str.split(",");
            session.setAttribute("con01",split[0]);
            session.setAttribute("con02",split[1]);
            str=str+","+count+","+price;
            ajaxd.setFlag(true);
            ajaxd.setMessage(str);
            session.setAttribute("shopCar",shopCar);

            //同步数据库
           if (customerinfo!=null){
               ShopCarEntity shopCarEntity = shopCarBiz.queryShopCar(customerinfo.getCid());
               Boolean aBoolean = shopCarBiz.deleteGoods(goodsEntity.getGid(), shopCarEntity.getScid());
           }
        }

        return ajaxd;
    }



    //获取商品总数量和总价格
    private String jiesuan(Map<String,GoodsEntity> map){
        Collection<GoodsEntity> values = map.values();
        Iterator<GoodsEntity> iterator = values.iterator();
        int allcount=0;
        float allprice=0;
        while(iterator.hasNext()){
            GoodsEntity next = iterator.next();
            allcount+=next.getGoodscount();
            allprice+=next.getGprice()*next.getGdiscount()*next.getGoodscount();
        }

        String strt=allcount+","+allprice;
        return strt;
    }
}
